﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGame {
    /// <summary>
    /// This Screen allows the user to str any volume controls for the game.
    /// Upon any volume changes, it updates the game volume as it is changed.
    /// </summary>
    class SoundOptionsScreen : MenuScreen {
        #region Fields

        MenuEntry back;
        MenuEntry masterVolumeEntry;
        MenuEntry musicVolumeEntry;
        MenuEntry sfxVolumeEntry;

        const int MASTER_VOLUME = 0;
        const int MUSIC_VOLUME = 1;
        const int SFX_VOLUME = 2;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructs the SoundOptionsScreen.
        /// </summary>
        public SoundOptionsScreen() : base("Sound Options") {
            masterVolumeEntry = new MenuEntry("Master Volume");
            musicVolumeEntry = new MenuEntry("Music Volume");
            sfxVolumeEntry = new MenuEntry("SFX Volume");
            back = new MenuEntry("Back");

            // Hook up menu event handlers.
            back.Selected += OnCancel;
            
            // Add entries to the menu.
            MenuEntries.Add(masterVolumeEntry);
            MenuEntries.Add(musicVolumeEntry);
            MenuEntries.Add(sfxVolumeEntry);
            MenuEntries.Add(back);

            UpdateText();
        }

        /// <summary>
        /// Updates the Text with all the current user settings in regards to 
        /// the current volume levels.
        /// </summary>
        void UpdateText() {
            int masterVolume = (int)Math.Round(Options.Default.master_volume * 100);
            String masterVolumeText = String.Format("Master Volume: - {0}% +", masterVolume);
            float musicVolume = (int)Math.Round(Options.Default.music_volume * 100);
            String musicVolumeText = String.Format("Music Volume: - {0}% +", musicVolume);
            float sfxVolume = (int)Math.Round(Options.Default.sfx_volume * 100);
            String sfxVolumeText = String.Format("SFX Volume: - {0}% +", sfxVolume);

            masterVolumeEntry.Text = masterVolumeText;
            musicVolumeEntry.Text = musicVolumeText;
            sfxVolumeEntry.Text = sfxVolumeText;
        }

        #endregion

        #region Input Handling

        /// <summary>
        /// Handles the input for the Sound Options. If any of the options are
        /// changed, the Screen saves to the settings file and updates the
        /// game volume accordingly.
        /// </summary>
        /// <param name="input"></param>
        public override void HandleInput(InputState input) {
            int playerIndex = (int)ControllingPlayer.Value;
            input.UpdateControllingPlayer(playerIndex);

            base.HandleInput(input);

            switch (SelectedEntry) {
                case MASTER_VOLUME:
                    if (input.IsRightButtonPressed()) {
                        Options.Default.master_volume = Math.Min(Options.Default.master_volume + 0.1f, 1.0f);
                    }
                    else if (input.IsLeftButtonPressed()) {
                        Options.Default.master_volume = Math.Max(Options.Default.master_volume - 0.1f, 0.0f);
                    }

                    break;
                case MUSIC_VOLUME:
                    if (input.IsRightButtonPressed()) {
                        Options.Default.music_volume = Math.Min(Options.Default.music_volume + 0.1f, 1.0f);
                    }
                    else if (input.IsLeftButtonPressed()) {
                        Options.Default.music_volume = Math.Max(Options.Default.music_volume - 0.1f, 0.0f);
                    }

                    break;
                case SFX_VOLUME:
                    if (input.IsRightButtonPressed()) {
                        Options.Default.sfx_volume = Math.Min(Options.Default.sfx_volume + 0.1f, 1.0f);
                    }
                    else if (input.IsLeftButtonPressed()) {
                        Options.Default.sfx_volume = Math.Max(Options.Default.sfx_volume - 0.1f, 0.0f);
                    }

                    break;
            }

            MyGame.UpdateSoundSettings();
            Options.Default.Save();
            UpdateText();
        }

        #endregion
    }
}
